<?php

namespace app\admin\controller;

use app\admin\model\CarBindStore;
use app\admin\model\PriceLog;
use app\BaseController;
use app\admin\model\User as UserModel;
use app\Request;

class User extends BaseController
{
    /* 列表 */
    public function getList()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $page = input('page', 1, 'intval');
        $perpage = 10;
        $content = input('searchContent');
        $where = [];
        if(!empty($this->userInfo['username']) && $this->userInfo['username'] !='admin'){
            $list = CarBindStore::getUserToManagerId($this->userInfo['id']);
            $where['id'] = $list? $list:[];
        }
        $result = UserModel::order('id desc')->where($where)->page($page, $perpage);
        if (!empty($content)) {
            $result->whereLike('wxname', "%$content%");
            $total = UserModel::whereLike('wxname', "%$content%")->where($where)->count();
        } else {
            $total = UserModel::where($where)->count();
        }
        $result = $result->select();
        $data = [
            'total' => $total,
            'current' => $page,
            'perpage' => $perpage,
            'data' =>  $result
        ];
        return show(config('status.success'), '会员列表', $data);
    }

    /* 余额变动记录 */
    public function getBlanceLog(Request $request)
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $where = [];
        if(!empty($this->userInfo['username']) && $this->userInfo['username'] !='admin'){
            $list = CarBindStore::getUserToManagerId($this->userInfo['id']);
            $where[] = ['user_id','in',$list??[]];
        }
        $wxname = $request->post('wxname', '');
        if(!empty($wxname)){
            $where[] = ['wxname','like',"%{$wxname}%"];
        }
        $store_id = $request->post('store_id',0);
        if(!empty($store_id)){
            $where[] = ['store_id','=',$store_id];
        }
        $manager_id = $request->post('manager_id',0);
        if(!empty($manager_id)){
            $where[] = ['manager_id','=',$manager_id];
        }
        $page = input('page', 1, 'intval');
        $perpage = 10;
        $content = input('searchContent');
        $result = PriceLog::alias('l')
            ->order('l.id desc')
            ->where($where)
            ->append(['event_type'])
            ->join('user u', 'u.id=l.user_id')
            ->field('l.*,u.wxname,u.wximage');
        if (!empty($content)) {
            $result->whereLike('u.wxname', "%$content%");
            $model = clone $result;
            $total = $model->count('id');
        } else {
            $model = clone $result;
            $total = $model->count();
        }
        $result = $result->page($page, $perpage)->select();
        //增加商户信息
        $result = \app\admin\model\Manager::getMananger($result);
        $data = [
            'total' => $total,
            'current' => $page,
            'perpage' => $perpage,
            'data' =>  $result
        ];
        return show(config('status.success'), '会员列表', $data);
    }

}
